package cn.gson.springboot.model.service;

import java.util.Date; 
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSONArray;

import cn.gson.springboot.model.dao.DestructionDetailsYkDAO;
import cn.gson.springboot.model.dao.DestructionYkDAO;
import cn.gson.springboot.model.dao.InventoryYkDAO;
import cn.gson.springboot.model.mapper.DestructionYkMapper;
import cn.gson.springboot.model.pojos.DestructionDetailsYk;
import cn.gson.springboot.model.pojos.DestructionYk;
import cn.gson.springboot.model.pojos.InventoryYk;
import cn.gson.springboot.model.vo.DestructionYkVO;
@Service
@Transactional(rollbackFor=Exception.class)
public class DestructionYkService {
	@Autowired
	private DestructionYkDAO desdao;
	@Autowired
	private DestructionDetailsYkDAO ddsdao;
	@Autowired
	private DestructionYkMapper desm;
	@Autowired
	private InventoryYkDAO itd;
	//查询所有
	public JSONArray findAll(Integer destruction_id){
		return desm.findAll(destruction_id);
	}
	
	//修改
	public DestructionYk idxg(Integer destructionId) {
		return desdao.idxgai(destructionId);
	}
	
	//新增
	public DestructionYk adddes(DestructionYk d) {
		return desdao.save(d);
	}
	
	//主从表新增
	public void saveOrder(DestructionYkVO desvo) {
		//获取采购订单进行新增
		DestructionYk dsstruction = desvo.getDsstruction();
		dsstruction.setDestructionStatic("未销毁");
		dsstruction.setDestructionTime(new Date());
		DestructionYk save = desdao.save(dsstruction);
		
		List<DestructionDetailsYk> details = desvo.getDdetailsYk();
		//循环
		for (DestructionDetailsYk pur : details) {
			pur.setDestructionYk(dsstruction);
			//查询库存
			InventoryYk inventory = itd.findById(pur.getInventoryYk().getInventoryYkId()).get();
			//重新赋值一下药品对象
			pur.setInventoryYk(inventory);
			pur.setDestructionDetaNumber(pur.getDestructionDetaNumber());
			ddsdao.save(pur);
		}
		//pddao.saveAll(details);
	}
	
}
